import re
import os
import json
import paramiko
import time
import threading
import sys
#import recordlog
import stat
import chardet
import datetime as dt
from socket import *

new_path = 'E:/agent/newgoal'
timeoutflag=False
downflag=False
# 定义一个类，表示一台远端linux主机


class Linux(object):
    # 通过IP，用户名，密码，超过时间初始化一个远程Linux主机
    def __init__(self, ip, username, password, timeout=30):
        self.ip = ip
        self.username = username
        self.password = password
        self.timeout = timeout
        self.t = ''
        self.chan = ''
        # 连接失败的重试次数
        self.try_times = 10

    # 调用该方法连接远程主机
    def connect(self):
        while True:
            # 连接过程中可能会抛出异常，比如网络不通，连接超时
            try:
                self.t = paramiko.Transport(sock=(self.ip, 22))
                self.t.connect(username=self.username, password=self.password)
                self.chan = self.t.open_session()
                self.chan.settimeout(self.timeout)
                self.chan.get_pty()
                self.chan.invoke_shell()
                # 如果没有抛出异常说明连接成功，直接返回
                # print(u'连接%s成功' % self.ip)
                # 接收到的网络数据解码为str
                # print(self.chan.recv(65535).decode('utf-8'))
                return
            # 这里不对可能的异常如socket.error,socket.timeout细化
            except Exception as e1:
                # print(e1)
                if self.try_times != 0:
                    # print(u'连接%s失败，进行重试' % self.ip)
                    self.try_times -= 1
                else:
                    # print(u'重试10次失败，结束程序')
                    sys.exit()

    # 断开连接
    def close(self):
        self.chan.close()
        self.t.close()

    # 发送要执行的指令
    def send(self, cmd):
        cmd += '\r'
        # 通过命令执行提示符来判断命令是否执行完成
        p = re.compile(r':~#')
        result = ''
        # 发送要执行的命令
        self.chan.send(cmd)
        while True:
            time.sleep(0.5)
            ret = self.chan.recv(65535)
            ret = ret.decode('utf-8')
            result += ret
            if p.search(ret):
                # print(result)
                return result

    # 获取远端linux主机上指定目录及其子目录下的所有文件
    def __get_all_files_in_remote_dir(self, sftp, remote_dir):
        # 保存所有文件的列表
        all_files = list()
        # 去掉路径字符串最后的字符'/'，如果有的话
        if remote_dir[-1] == '/':
            remote_dir = remote_dir[0:-1]
        # 获取当前指定目录下所有的目录及文件，包含属性值
        files = sftp.listdir_attr(remote_dir)
        for x in files:
            # remote_dir目录中每个文件或目录的完整路径
            filename = remote_dir+'/'+x.filename
            # 如果是目录，则递归处理该目录，这里用到了stat库中S_ISDIR方法，与linux中宏的名字完全一致
            if stat.S_ISDIR(x.st_mode):
                all_files.extend(
                    self.__get_all_files_in_remote_dir(sftp, filename))
            else:
                all_files.append(filename)
        return all_files

    def sftp_get_dir(self, remote_dir, local_dir):
        t = paramiko.Transport(sock=(self.ip,22))
        t.connect(username=self.username, password=self.password)
        sftp = paramiko.SFTPClient.from_transport(t)
        # 获取远端linux主机上指定目录及其子目录下的所有文件
        all_files = self.__get_all_files_in_remote_dir(sftp, remote_dir)
        # 依次get每个文件
        for x in all_files:
            filename = x.split('/')[-1]
            local_filename = os.path.join(local_dir, filename)
            # print(u'get文件%s传输中...' % filename)
            sftp.get(x, local_filename)

    # 获取本地指定目录以及其子目录下的所有文件
    def __get_all_files_in_local_dir(self, local_dir):
        # 保存所有文件的列表
        all_files = list()
        # 获取当前指定目录下的所有目录及文件，包含属性值
        files = os.listdir(local_dir)
        for x in files:
            # local_dir目录中每一个文件或目录的完整路径
            filename = os.path.join(local_dir, x)
            # 如果是目录，则递归处理该目录
            if os.path.isdir(x):
                all_files.extend(self.__get_all_files_in_local_dir(filename))
            else:
                all_files.append(filename)
        return all_files

    def sftp_put_dir(self, local_dir, remote_dir):
        t = paramiko.Transport(sock=(self.ip, 22))
        t.connect(username=self.username, password=self.password)
        sftp = paramiko.SFTPClient.from_transport(t)
        # 去掉路径字符串最后的字符'/',如果有的话
        if remote_dir[-1] == '/':
            remote_dir = remote_dir[0:-1]
        # 获取本地指定目录及其子目录下的所有文件
        all_files = self.__get_all_files_in_local_dir(local_dir)
        # 依次put每个文件
        for x in all_files:
            filename = os.path.split(x)[-1]
            remote_filename = remote_dir+'/'+filename
            # print(u'Put文件%s传输中...' % filename)
            sftp.put(x, remote_filename)


class Dispacher(threading.Thread):
    def __init__(self, fun):
        threading.Thread.__init__(self)
        self.setDaemon(True)
        self.result = None
        self.error = None
        self.fun = fun
        self.start()

    def run(self):
        try:
            self.result = self.fun()
        except:
            self.error = sys.exc_info()


def ssh(ip, username, passwd, cmd):
    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(ip, 22, username, passwd)
        print('ssh 108 ok')
        time.sleep(1) #10 d:60
        stdin, stdout, stderr = ssh.exec_command(cmd)
        stdout.read()
        ssh.close()
    except:
        print('%s\tError\n' % (ip))
        pass

def wrBIOS(BIOS):
    if (os.path.exists(r'E:/agent/newbios/bios.txt')):
        os.remove(r'E:/agent/newbios/bios.txt')
    wfile = open('E:/agent/newbios/bios.txt', 'w')
    bios_list = json.dumps(str(BIOS))
    wfile.write(bios_list)
    wfile.close()

    rfile = open('E:/agent/newbios/bios.txt', 'r')
    list_bios = rfile.read()
    newbios = eval(list_bios)
    num = re.findall('\d+', newbios)
    list_to_bios = list(map(int, num))
    # print(list_to_bios)

# 寄存器默认二进制
#[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    # l3t_static_ctrl=[0,0,0,0,0,1,0,0,0,0,1,1,0,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0] 0x0430 C800
    # l3t_dynamic_ctrl=[1,1,0,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,1,0,0,1,1,1,1,0,1] 0xDDFF 973D
    # l3t_dynamic_auctrl0=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,0,1,1,1,1] 0X0000 706F
    # l3t_dynamic_auctrl1=[1,1,1,1,0,0,1,1,0,1,1,1,0,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,1,0] 0xF377 3CFE ..
    # l3t_perfecth=[1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0] 0xFF7D B008
    # l3t_prime_num_config1=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1]
    # hha_dir_ctrl=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,1,1,1,0,0,1,0,1,0,0] 0X0002 0B94 ..
    # hha_func_dis=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] 0x0000 0000 ..
    # mn_dynamic_ctrl=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,1,0] 0x0000 0556 ..
    # hha_totemnum=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1] 0x13
    # hha_canum_l=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
    # hha_canum_h=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    # hha_ddr_level=[0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,0,0,0,0]
    # l3t_prime_num_config_2=[0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1]
    # l3t_prime_num_config=[0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1]
# 增加可配置项置1
    l3t_static_ctrl = [0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0,
                       0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    l3t_dynamic_ctrl = [1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1]
    l3t_dynamic_auctrl0 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                           0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1]
    l3t_dynamic_auctrl1 = [1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1,
                           0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]
    l3t_perfecth = [1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
                    1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0]
    #l3t_prime_num_config1 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
    hha_dir_ctrl = [0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1]
    hha_func_dis = [0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,0]
    mn_dynamic_ctrl = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0]
    hha_totemnum=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1]
    # hha_canum_l=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
    # hha_canum_h=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
    # hha_ddr_level=[0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1]
    # l3t_prime_num_config_2=[]
    # l3t_prime_num_config=[]

# 人工经验值   对应位数为31-x
    l3t_static_ctrl_change = [1, 2, 3, 5, 6, 7, 15, 17]
    l3t_dynamic_ctrl_change = [4, 5, 6, 7, 12, 13,
                               14, 15, 16, 17, 18, 20, 21, 23, 30, 31]
    l3t_dynamic_auctrl0_change = [13, 18, 19, 23, 24, 28, 30, 31]
    l3t_dynamic_auctrl1_change = [
        0, 2, 3, 5, 6, 7, 11, 13, 14, 15, 17, 19, 20, 21, 25, 26, 27, 28, 29, 30, 31]
    l3t_perfecth_change = [13, 14, 15, 16, 17, 18, 19, 24]
    #l3t_prime_num_config1_change = [16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]
    hha_dir_ctrl_change = [13,14,15,16,17,18,19,20,21,22,25,28,29,30,31] #对应不配置位 0-12 24 26 27 (16)
    hha_func_dis_change = [12,13,14,15,16,17,18,20,21,22,23,24,25,26,27,28,29] #[30,31]不可配 #对应不配置位 0-7,8-11,19  (19)
    mn_dynamic_ctrl_change = [20,21,22,23,24,25,26,27,28,29,30]#对应不配置位 0-19 31 (11)
    # hha_totemnum_change=[30]不可配 #只能配置30 (1)
    # hha_canum_l_change=[]
    # hha_canum_h_change=[]
    # hha_ddr_level_change=[]
    # l3t_prime_num_config_2_change=[]
    # l3t_prime_num_config_change=[]

    i = 0
    while i < 104:
        for k1 in range(len(l3t_static_ctrl_change)):
            l3t_static_ctrl[l3t_static_ctrl_change[k1]
                            ] = l3t_static_ctrl[l3t_static_ctrl_change[k1]] & list_to_bios[i]
            i += 1
            k1 += 1
        for k2 in range(len(l3t_dynamic_ctrl_change)):
            l3t_dynamic_ctrl[l3t_dynamic_ctrl_change[k2]
                             ] = l3t_dynamic_ctrl[l3t_dynamic_ctrl_change[k2]] & list_to_bios[i]
            i += 1
            k2 += 1
        for k3 in range(len(l3t_dynamic_auctrl0_change)):
            l3t_dynamic_auctrl0[l3t_dynamic_auctrl0_change[k3]
                                ] = l3t_dynamic_auctrl0[l3t_dynamic_auctrl0_change[k3]] & list_to_bios[i]
            i += 1
            k3 += 1
        for k4 in range(len(l3t_dynamic_auctrl1_change)):
            l3t_dynamic_auctrl1[l3t_dynamic_auctrl1_change[k4]
                                ] = l3t_dynamic_auctrl1[l3t_dynamic_auctrl1_change[k4]] & list_to_bios[i]
            i += 1
            k4 += 1
        for k5 in range(len(l3t_perfecth_change)):
            l3t_perfecth[l3t_perfecth_change[k5]
                         ] = l3t_perfecth[l3t_perfecth_change[k5]] & list_to_bios[i]
            i += 1
            k5 += 1
        # for k6 in range(len(l3t_prime_num_config1_change)):
        #     l3t_prime_num_config1[l3t_prime_num_config1_change[k6]
        #                           ] = l3t_prime_num_config1[l3t_prime_num_config1_change[k6]] & list_to_bios[i]
        #     i += 1
        #     k6 += 1
        for k7 in range(len(hha_dir_ctrl_change)):
            hha_dir_ctrl[hha_dir_ctrl_change[k7]
                         ] = hha_dir_ctrl[hha_dir_ctrl_change[k7]] & list_to_bios[i]
            i += 1
            k7 += 1
        for k8 in range(len(hha_func_dis_change)):
            hha_func_dis[hha_func_dis_change[k8]
                         ] = hha_func_dis[hha_func_dis_change[k8]] & list_to_bios[i]
            i += 1
            k8 += 1
        for k9 in range(len(mn_dynamic_ctrl_change)):
            mn_dynamic_ctrl[mn_dynamic_ctrl_change[k9]
                            ] = mn_dynamic_ctrl[mn_dynamic_ctrl_change[k9]] & list_to_bios[i]
            i += 1
            k9 += 1
        # for k10 in range(len(hha_totemnum_change)):
        #     hha_totemnum[hha_totemnum_change[k10]
        #                     ] = hha_totemnum[hha_totemnum_change[k10]] & list_to_bios[i]
        #     i += 1
        #     k10 += 1

    l3t_static_ctrl_hex = hex(
        int(''.join(map(lambda x: str(int(x)), l3t_static_ctrl)), 2))
    l3t_dynamic_ctrl_hex = hex(
        int(''.join(map(lambda x: str(int(x)), l3t_dynamic_ctrl)), 2))
    l3t_dynamic_auctrl0_hex = hex(
        int(''.join(map(lambda x: str(int(x)), l3t_dynamic_auctrl0)), 2))
    l3t_dynamic_auctrl1_hex = hex(
        int(''.join(map(lambda x: str(int(x)), l3t_dynamic_auctrl1)), 2))
    l3t_perfecth_hex = hex(
        int(''.join(map(lambda x: str(int(x)), l3t_perfecth)), 2))
    # l3t_prime_num_config1_hex = hex(
    #     int(''.join(map(lambda x: str(int(x)), l3t_prime_num_config1)), 2))
    hha_dir_ctrl_hex = hex(
        int(''.join(map(lambda x: str(int(x)), hha_dir_ctrl)), 2))
    hha_func_dis_hex = hex(
        int(''.join(map(lambda x: str(int(x)), hha_func_dis)), 2))
    mn_dynamic_ctrl_hex = hex(
        int(''.join(map(lambda x: str(int(x)), mn_dynamic_ctrl)), 2))
    # hha_totemnum_hex = hex(
    #     int(''.join(map(lambda x: str(int(x)), hha_totemnum)), 2))

    out = [l3t_static_ctrl_hex, l3t_dynamic_ctrl_hex, l3t_dynamic_auctrl0_hex, l3t_dynamic_auctrl1_hex,
           l3t_perfecth_hex, hha_dir_ctrl_hex, hha_func_dis_hex, mn_dynamic_ctrl_hex]

    for x in range(len(out)):
        print(out[x])
    return out


def hexBIOS(out):
    if(os.path.exists(r'E:/agent/newhexbios/hexbios.txt')):
        os.remove(r'E:/agent/newhexbios/hexbios.txt')
    wfile = open('E:/agent/newhexbios/hexbios.txt', 'w')
    for x in out:
        wfile.write(x+'\n')
    #     recordlog.logwrite().write(x+'\n')
    # recordlog.logwrite().close()
    wfile.close()
    OK = 1
    if(os.path.exists(r'E:/agent/newhexbios/hexbios.txt')):
        return OK
    else:
        return None

def powerset():
    for a in range(1, 3):
        pingfile = os.popen('ping 192.168.1.108 -n 1').read()
        pingok = re.compile(r'字节=32', re.M).findall(pingfile)
        sshok=True
        if pingok:
            try:
                ssh = paramiko.SSHClient()
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                ssh.connect("192.168.1.108",22, "root", "Admin@9000", timeout=10)
                ssh.close()
            except Exception:
                sshok=False
                # print('ssh 192.168.1.108 is fail')
        else:
            # print('ping 192.168.1.108 is fail')
            pass
        if pingok and sshok:
            # print('ping&ssh 192.168.1.108 is ok')
            pass
        else:
            global downflag
            ssh = paramiko.SSHClient()
            know_host = paramiko.AutoAddPolicy()
            ssh.set_missing_host_key_policy(know_host)
            ssh.connect(
                hostname="192.168.1.123",
                port=22,
                username="Administrator",
                password="Admin@9000"
            )
            execmd = "bash --login -c 'ipmcset -d powerstate -v 2'"
            stdin, stdout, stderr = ssh.exec_command(execmd)
            time.sleep(1) # d:10
            stdin.write("Y")
            powerdown = str(stdout.read(), 'utf-8')
            # print(powerdown)
            time.sleep(10) # d:60

            execmd = "bash --login -c 'ipmcset -d powerstate -v 1'"
            stdin, stdout, stderr = ssh.exec_command(execmd)
            time.sleep(1) # d:10
            stdin.write("Y")
            powerout = str(stdout.read(), 'utf-8')
            # print(powerout)
            downflag=True
            wait_reset_finished()
            ssh.close()


def poweron():
    ssh = paramiko.SSHClient()
    know_host = paramiko.AutoAddPolicy()
    ssh.set_missing_host_key_policy(know_host)
    ssh.connect(
        hostname="192.168.1.123",
        port=22,
        username="Administrator",
        password="Admin@9000"
    )
    execmd = "bash --login -c 'ipmcget -d powerstate'"
    stdin, stdout, stderr = ssh.exec_command(execmd)
    powerout = str(stdout.read(), 'utf-8')
    # print(powerout)
    poweron = re.compile(r'On', re.M).findall(powerout)
    if poweron:
        # print('power on')
        pass
    else:
        execmd1 = "bash --login -c 'ipmcset -d powerstate -v 1'"
        stdin1, stdout1, stderr1 = ssh.exec_command(execmd1)
        time.sleep(1) # d:10
        stdin1.write("Y")
        poweryes = str(stdout1.read(), 'utf-8')
        # print(poweryes)
    powerset()
    ssh.close()

def wait_reset_finished():
    print('--等待服务器重启中...--')
    time.sleep(200)
    a=True
    while a:
        pingfile = os.popen('ping 192.168.1.108 -n 1').read()
        pingok = re.compile(r'字节=32', re.M).findall(pingfile)
        if pingok:
            a=False
        time.sleep(5) #每隔5s ping一次服务器，直到重启成功
    print('--服务器重启成功！--')


def wrGoal():
    if(os.path.exists(r'E:/agent/newgoal/result.txt')):
        os.remove(r'E:/agent/newgoal/result.txt')
    execmd = "cd /home/benchmarks/STREAM-master/;bash test2.sh"
    ssh("192.168.1.108", "root", "Admin@9000", execmd)
    # print("wrGoal end")


def reGoal(new_path):
    matcher2 = 0
    goallist = []
    for derName, subfolders, filenames in os.walk(new_path):
        for i in range(len(filenames)):
            if filenames[i].endswith('.txt'):
                file_path = derName+'/'+filenames[i]
                for line in open(file_path):
                    # print(line)
                    pattern1 = re.compile('^[A-Za-z]+$')
                    pattern2 = re.compile('^(\-|\+)?\d+(\.\d+)?$')
                    if pattern2.findall(line):
                        matcher2 = re.findall('([0-9]\d*\.?\d*)', line)
                        goallist.append(matcher2)
                    elif re.match(r'(.*)fail(.*).*?', line, re.I):
                        goallist.append(['fail'])
                    elif re.match(r'(.*)success(.*).*?', line, re.I):
                        goallist.append(['success'])
                    elif pattern1.findall(line):
                        matcher2 = re.findall('^[A-Za-z]+$', line)
                        goallist.append(matcher2)
                #print(goallist)
                #goallist_str = ''.join(str(i) for i in goallist)
                return goallist

def put_shell():
    shell_path_local =r'shell'
    shell_path_remote =r'/home/benchmarks/STREAM-master'
    host = Linux('192.168.1.108', 'root', 'Admin@9000')
    host.sftp_put_dir(shell_path_local, shell_path_remote)
    print('update shell ok')

def sim(BIOS):
    remote_path1 = r'/home/server/newhexbios'
    local_path1 = r'E:\agent\newhexbios'
    remote_path2 = r'/home/server/newgoal'
    local_path2 = r'E:\agent\newgoal'
    host = Linux('192.168.1.108', 'root', 'Admin@9000')
    #goal = []
    goalout = []
    print('\n-----开始时间：',dt.datetime.now(),'-----')
    global timeoutflag
    global downflag
    timeoutflag=False #设置flag为正常
    downflag=False #设置flag为正常
    poweron() #判断服务器是否工作正常，不正常则开机/强制重启
    BIOS16 = wrBIOS(BIOS)
    if BIOS16 != None:
        OK = hexBIOS(BIOS16)
        if OK != None:
            # print('exr wrGoal')
            try:
                host.sftp_put_dir(local_path1, remote_path1)
            except Exception:
                try:
                    host.sftp_put_dir(local_path1, remote_path1)
                except Exception:
                    timeoutflag=True
                    # print("sftp_put_error")
            else:
                # print("sftp_put_ok")
                pass
            if timeoutflag==False: 
                runshell = Dispacher(wrGoal)
                runshell.join(60)
                if runshell.isAlive():
                    runshell2 = Dispacher(wrGoal)
                    runshell2.join(60)
                    if runshell2.isAlive():
                        timeoutflag=True
                        print("wrGoal_timeout")
            if timeoutflag==False:
                try:
                    host.sftp_get_dir(remote_path2, local_path2)
                except Exception:
                    try:
                        host.sftp_get_dir(remote_path2, local_path2)
                    except Exception:
                        timeoutflag=True
                        # print("sftp_get_error")
                else:
                    # print("sftp_get_ok")
                    pass
            if timeoutflag==True:
                print('-----连接超时-----')
                pass
            powerset()
            if downflag==True:
                goalout=["DownError"]
            elif timeoutflag==True:
                goalout=["TimeoutError"]
            else:
                print("threadwrGoal ok")
                goalout = reGoal(new_path)
                #goalout = list(map(lambda x: float(x), goal[0]))
    #powerset()
    # print('输出result：',goalout)
    # recordlog.logwrite().write(str(goalout[0])+'\n')
    # recordlog.logwrite().write(str(goalout[-1])+'\n')
    # recordlog.logwrite().close()
    return goalout
